package com.java.jvm.bytecode;

import javassist.*;

import java.io.IOException;

public class JavassistCreateClassTests {
    public static void main(String[] args) throws CannotCompileException, IOException {
        //创建新的字节码类型
        CtClass ctClass =
                ClassPool.getDefault().makeClass("com.java.jvm.WelcomeJVM");
        //设置类的访问修饰符
        ctClass.setModifiers(Modifier.PUBLIC);

        //构建新的类的成员变量
        CtField ctFieldNew = new CtField(CtClass.intType,"age",ctClass);
        //设置类的访问修饰符为public
        ctFieldNew.setModifiers(Modifier.PUBLIC);
        //将属性添加到类中
        ctClass.addField(ctFieldNew);

        ctClass.writeFile("01-java/target/classes/");
    }
}
